package com.colony.networkvm;

import com.argot.TypeException;
import com.argot.TypeLibrary;
import com.argot.remote.MetaLocation;
import com.argot.remote.MetaObject;
import com.colony.networkvm.machine.NvmState;
import com.colony.remote.RemoteException;
import com.colony.remote.rpc.RpcDelegate;
import com.colony.remote.rpc.RpcMetaMethodRequest;

public class NvmHostSkeleton 
implements RpcDelegate
{
	private int _processId;
	private int _getObjectId;
	
	public NvmHostSkeleton(TypeLibrary library)
	throws TypeException
	{
		_processId = library.getId("nvm.host.process");
		_getObjectId = library.getId("nvm.host.getobject");
	}
	
	public void processRequest(RpcMetaMethodRequest request, Object target)
	throws RemoteException 
	{
		INvmHost host = (INvmHost) target;
		Object[] args = request.getArguments();
		
		if (request.getMetaMethod().getMemberTypeId() == _processId )
		{
			NvmState state = host.process((NvmState)args[0]);
			request.setReturnValue(state);
		}
		else if (request.getMetaMethod().getMemberTypeId() == _getObjectId)
		{
			MetaObject object = host.getObject((MetaLocation)args[0]);
			request.setReturnValue(object);
		}
		else
		{
			throw new RemoteException("NvmHostSkeleton: method not found");
		}
	}

}
